var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
    hiddenmodalput: true,
		paypwd:'',
		couponList: [],
		couponrid:0,
		coupontype:1,
		usescore: 0,
		money: 0,
		disprice: 0,
		dkmoney:0,
		couponmoney:0,
		paymoney: 0,
		mdkey:0,
		couponvisible:false
	},
	_onLoad: function () {
		var that = this;
		//获取产品信息
		$.get('wxpay/maidan', { }, function (res) {
			if (res.status == 0) {
				$.alert(res.msg,function(){
					$.goback();
				})
				return;
			}
			var userinfo = res.userinfo
			var couponList = res.couponList
			var mdlist = res.mdlist
			that.setData(res);
			if(mdlist.length>0){
				wx.getLocation({
					type: 'gcj02',
					success:function(res) {
						var latitude = res.latitude
						var longitude = res.longitude
						var speed = res.speed
						var accuracy = res.accuracy
						for(var i in mdlist){
							mdlist[i].juli = that.GetDistance(latitude,longitude,mdlist[i].latitude,mdlist[i].longitude);
						}
						mdlist = mdlist.sort(that.compare('juli'));
						console.log(mdlist)
						that.setData({mdlist:mdlist});
				 }
				})
			}
		});
	},
	//选择门店
	selectmd:function(e){
		var that = this;
		var itemlist = [];
		var mdlist = this.data.mdlist;
		for(var i=0;i<mdlist.length;i++){
			itemlist.push(mdlist[i].name + (mdlist[i].juli ? ' 距离:'+mdlist[i].juli+'千米' : ''));
		}
		wx.showActionSheet({
			itemList:itemlist,
			success:function(res) {
				that.setData({ mdkey: res.tapIndex });
			}
		});
	},
	//积分抵扣
	scoredk: function (e) {
		var usescore = e.detail.value[0];
		if (!usescore) usescore = 0
		this.setData({ usescore: usescore });
		this.calculatePrice();

	},
	inputMoney: function (e) {
		console.log(e)
		var money = e.detail.value;
		if(!money) money = 0
		var money = parseFloat(money);
		if(money <=0) money = 0;
		this.setData({
			money: money
		})
		this.calculatePrice();
	},
	getpwd:function(e){
		var paypwd = e.detail.value;
		this.setData({
			paypwd: paypwd
		});
	},
	cancel: function () {
		this.setData({
			hiddenmodalput: true
		});
	},
	modalinput: function () {
		this.setData({
			hiddenmodalput: !this.data.hiddenmodalput
		})
	},
	//计算价格
	calculatePrice: function () {
		var that = this;
		var money = parseFloat(that.data.money);	//+商品总价
		if(that.data.userinfo.discount>0 && that.data.userinfo.discount<10){
			var disprice = Math.round(money * (1-0.1*that.data.userinfo.discount)*100)/100;			//-会员折扣
		}else{
			var disprice = 0;
		}
		if (that.data.usescore) {
			var dkmoney = parseFloat(that.data.userinfo.dkmoney);			//-积分抵扣
		} else {
			var dkmoney = 0;
		}
		var couponmoney = parseFloat(that.data.couponmoney)	//-优惠券抵扣 
		
		var paymoney = money - disprice - couponmoney - dkmoney; // 商品金额 - 会员折扣 - 优惠券抵扣 - 积分抵扣
		if(paymoney < 0) paymoney = 0;

		paymoney = paymoney.toFixed(2)
		that.setData({ paymoney: paymoney, disprice: disprice })
	},
	chooseCoupon:function(e){
		var couponrid = e.currentTarget.dataset.id;
		var couponkey = e.currentTarget.dataset.key;
		if(couponrid == this.data.couponrid){
			this.setData({ couponkey: 0,couponrid:0,coupontype:1,couponmoney: 0,couponvisible:false});
		}else{
			var couponList = this.data.couponList;
			var couponmoney = couponList[couponkey]['money']
			var coupontype = couponList[couponkey]['type']
			if(coupontype==4){
				couponmoney = this.data.freightprice
			}
			this.setData({ couponkey: couponkey,couponrid:couponrid,coupontype:coupontype, couponmoney: couponmoney,couponvisible:false});
		}
		this.calculatePrice();
	},
	topay: function (e) {
		var that = this
		//var typeid = e.detail.value.paytype;
    var typeid = e.currentTarget.dataset.typeid;
		var money = that.data.money
		var couponrid = that.data.couponrid
		var usescore = that.data.usescore
		if((that.data.mdlist).length>0){
			var mdid = that.data.mdlist[that.data.mdkey].id;
		}else{
			var mdid = 0;
		}
		if (typeid == 2) {
			that.setData({hiddenmodalput:true})
			$.confirm('确定用'+(that.data.textset['余额'] || '余额')+'支付吗?', function () {
				$.post('wxpay/maidan', {typeid: typeid,paypwd:that.data.paypwd,money:money,couponrid:couponrid,usescore:usescore,mdid:mdid}, function (data) {
					if (data.status == 0) {
						$.error(data.msg);
						return;
					}
					if (data.status == 2) { //无需付款
						$.success(data.msg);
						setTimeout(function () {
							$.goto('/pages/my/maidanlog');
						}, 1000);
						return;
					}
				}, '提交中');
			})
		} else {
			$.post('wxpay/maidan', {typeid: typeid,money:money,couponrid:couponrid,usescore:usescore,mdid:mdid}, function (data) {
				if (data.status == 0) {
					$.error(data.msg);
					return;
				}
				if (data.status == 2) { //无需付款
					$.success(data.msg);
					setTimeout(function () {
						$.goto('/pages/my/maidanlog');
					}, 1000);
					return;
				}
				var opt = data.data;
				wx.requestPayment({
					'timeStamp': opt.timeStamp,
					'nonceStr': opt.nonceStr,
					'package': opt.package,
					'signType': 'MD5',
					'paySign': opt.paySign,
					'success': function (res) {
						$.success('付款完成');
						setTimeout(function () {
							$.goto('/pages/my/maidanlog');
						}, 1000)
					},
					'fail': function (res) { }
				});
			}, '提交中');
		}
	},
	showCouponList:function(){
		this.setData({couponvisible:true})
	},
	handleClickMask:function(){
		this.setData({couponvisible:false})
	},
	GetDistance:function(lat1,  lng1,  lat2,  lng2){
		var radLat1 = lat1*Math.PI / 180.0;
		var radLat2 = lat2*Math.PI / 180.0;
		var a = radLat1 - radLat2;
		var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
		Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
		s = s *6378.137 ;// EARTH_RADIUS;
		s = Math.round(s * 100) / 100;
		return s;
	},
	compare:function(property){
		return function(a,b){
			var value1 = a[property];
			var value2 = b[property];
			return value1 - value2;
		}
	}
}));